package com.ziyun.erp.web.api.quoteFastPrintGroupPrice;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.utility.StringUtils;
import com.ziyun.erp.common.utils.JsonUtils;
import com.ziyun.erp.modules.quoteFastPrintGroupPrice.entity.QuoteFastPrintGroupPriceEntity;
import com.ziyun.erp.modules.quoteFastPrintGroupPrice.service.QuoteFastPrintGroupPriceService;
import io.renren.common.annotation.SysLog;
import io.renren.common.utils.R;
import io.renren.common.validator.ValidatorUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;


/**
 * 快印报价组合计价表
 *
 * @author llc
 * @email 
 * @date 2018-09-19 15:23:53
 */
@RestController
@RequestMapping("port/quoteFastPrintGroupPrice/quotefastprintgroupprice")
public class QuoteFastPrintGroupPriceApi {
    @Autowired
    private QuoteFastPrintGroupPriceService quoteFastPrintGroupPriceService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestBody Map<String, Object> paramBodyMap){
        //解析数据
        QuoteFastPrintGroupPriceEntity quoteFastPrintGroupPrice=JsonUtils.parseMap2Object((Map)paramBodyMap.get("quoteFastPrintGroupPrice"),QuoteFastPrintGroupPriceEntity.class);
        //查看详情
        List<QuoteFastPrintGroupPriceEntity> fastPrintGroupPriceData =quoteFastPrintGroupPriceService.detail(quoteFastPrintGroupPrice);
        return R.ok().put("fastPrintGroupPriceData", fastPrintGroupPriceData);
    }

    /**
     * 信息
     */
    @RequestMapping("/info")
    public R info(@RequestBody Map<String, Object> paramBodyMap){
       Integer id = (Integer) paramBodyMap.get("id");
        QuoteFastPrintGroupPriceEntity quoteFastPrintGroupPrice = quoteFastPrintGroupPriceService.selectById(id);
        return R.ok().put("quoteFastPrintGroupPrice", quoteFastPrintGroupPrice);
    }


     /**
     * 批量保存
     */
    @SysLog("批量保存组合配价")
    @RequestMapping("/save")
    public R save(@RequestBody Map<String, Object> paramBodyMap){
        String updateUser=paramBodyMap.get("update_user").toString();
        List<QuoteFastPrintGroupPriceEntity> groupPriceList = JsonUtils.string2Collection(paramBodyMap.get("groupPriceList").toString(),List.class,QuoteFastPrintGroupPriceEntity.class);
        for (int i=0;i<groupPriceList.size();i++){
            QuoteFastPrintGroupPriceEntity quoteFastPrintGroupPrice=  groupPriceList.get(i);
            //设置参数
            quoteFastPrintGroupPrice.setUpdateDate(new Date());
            quoteFastPrintGroupPrice.setUpdateUser(updateUser);
        }
        //批量更新
        quoteFastPrintGroupPriceService.updateBatchById(groupPriceList);
        return R.ok();
    }


    /**
     * 修改
     */
    @SysLog("修改组合配价")
    @RequestMapping("/update")
    public R update(@RequestBody QuoteFastPrintGroupPriceEntity quoteFastPrintGroupPrice){
        ValidatorUtils.validateEntity(quoteFastPrintGroupPrice);
        quoteFastPrintGroupPriceService.updateAllColumnById(quoteFastPrintGroupPrice);//全部更新
        
        return R.ok();
    }

    /**
     * 删除
     */
    @SysLog("删除组合配价")
    @RequestMapping("/delete")
    public R delete(@RequestBody Map paramBodyMap){
        List<Integer> ids = JsonUtils.string2Collection(paramBodyMap.get("ids").toString(), List.class, Integer.class);
       String updateName=paramBodyMap.get("updateName").toString();
        return   quoteFastPrintGroupPriceService.updateDelFlag(ids,updateName);
    }

    /**
     * 初始化已存在的颜色和材料列表
     */
    @RequestMapping("/fastPrintMaterialList")
    public R getFastPrintMaterialList(@RequestBody Map<String, Object> paramBodyMap){
        List<QuoteFastPrintGroupPriceEntity> fastPrintMaterialData=quoteFastPrintGroupPriceService.getFastPrintMaterialList(paramBodyMap);
        return R.ok().put("fastPrintMaterialData", fastPrintMaterialData);
    }

    /**
     * 查找已存在的颜色和材料列表
     */
    @RequestMapping("/queryfastPrintMaterialList")
    public R queryFastPrintMaterialList(@RequestBody Map<String, Object> paramBodyMap){
        QuoteFastPrintGroupPriceEntity quoteFastPrintGroupPrice=JsonUtils.string2Object(paramBodyMap.get("quoteFastPrintGroupPrice").toString(),QuoteFastPrintGroupPriceEntity.class);
        Integer sellId= Integer.parseInt( paramBodyMap.get("sellId").toString());
        Map<String,Object> params=new HashMap<String,Object>();
        if(StringUtils.isNotBlank(quoteFastPrintGroupPrice.getOptionName())){
            params.put("optionName",quoteFastPrintGroupPrice.getOptionName().trim());
        }
       if(StringUtils.isNotBlank(quoteFastPrintGroupPrice.getColour())){
            params.put("colour",quoteFastPrintGroupPrice.getColour().trim());
        }
        params.put("factory_id",sellId);
        List<QuoteFastPrintGroupPriceEntity> fastPrintMaterialData=quoteFastPrintGroupPriceService.getFastPrintMaterialList(params);
        return R.ok().put("fastPrintMaterialData", fastPrintMaterialData);
    }

}
